package net.minecraft.src;

import java.util.Random;

public class BlockPrehistoricSapling extends BlockFlower
{

    protected BlockPrehistoricSapling(int i, int j)
    {
        super(i, j);
        float f = 0.4F;
        setBlockBounds(0.5F - f, 0.0F, 0.5F - f, 0.5F + f, f * 2.0F, 0.5F + f);
		blockIndexInTexture = ModLoader.addOverride("/terrain.png", "/pph/lepidodendronSapling.png");
		calamites = ModLoader.addOverride("/terrain.png", "/pph/calamitesSapling.png");
		cordaites = ModLoader.addOverride("/terrain.png", "/pph/cordaitesSapling.png");
    }
	
	public boolean canPlaceBlockAt(World world, int i, int j, int k)
    {
        return super.canPlaceBlockAt(world, i, j, k) && canThisPlantGrowOnThisBlockID(world.getBlockId(i, j - 1, k));
    }

    protected boolean canThisPlantGrowOnThisBlockID(int i)
    {
        return i == mod_pph.prehistoricGrass.blockID || i == mod_pph.prehistoricDirt.blockID;
    }

    public void updateTick(World world, int i, int j, int k, Random random)
    {
        if(world.multiplayerWorld)
        {
            return;
        }
        super.updateTick(world, i, j, k, random);
        if(world.getBlockLightValue(i, j + 1, k) >= 9 && random.nextInt(7) == 0)
        {
            int l = world.getBlockMetadata(i, j, k);
            if((l & 8) == 0)
            {
                world.setBlockMetadataWithNotify(i, j, k, l | 8);
            } else
            {
                growTree(world, i, j, k, random);
            }
        }
    }

    public int getBlockTextureFromSideAndMetadata(int i, int j)
    {
        j &= 3;
        if(j == 1 || j == 4 || j == 7 || j == 10 || j == 13)
        {
            return calamites;
        }
        if(j == 2 || j == 5 || j == 8 || j == 11 || j == 14)
        {
            return cordaites;
        } else
        {
            return super.getBlockTextureFromSideAndMetadata(i, j);
        }
    }

    public void growTree(World world, int i, int j, int k, Random random)
    {
        int l = world.getBlockMetadata(i, j, k) & 3;
        world.setBlock(i, j, k, 0);
        Object obj = null;
        if(l == 1)
        {
            obj = new PPHGenCalamites();
        } else
        if(l == 2)
        {
            obj = new PPHGenCordaites();
        } else
        {
            obj = new PPHGenLepidodendron();
            if(random.nextInt(10) == 0)
            {
                obj = new PPHGenSigillaria();
            }
        }
        if(!((WorldGenerator) (obj)).generate(world, random, i, j, k))
        {
            world.setBlockAndMetadata(i, j, k, blockID, l);
        }
    }

    protected int damageDropped(int i)
    {
        return i & 3;
    }

	private int calamites;
	private int cordaites;
}
